package com.ete.service.util

import android.view.View
import android.view.Window
import android.view.WindowManager
import com.blankj.utilcode.util.ScreenUtils
import com.blankj.utilcode.util.SizeUtils
import com.blankj.utilcode.util.ThreadUtils

/**
 * Author:LiXian
 * <p>
 * Desc:TODO
 * Version:1.0
 * date 2024/1/15 15:55
 */
object ServiceUtil {

    @JvmStatic
    fun <T> executeBySingleTask(doInBackground: () -> T, onSuccess: ((T) -> Unit)? = null, onFail: ((String) -> Unit)? = null) {
        val mTask = object : ThreadUtils.SimpleTask<T>() {
            override fun doInBackground(): T {
                return doInBackground.invoke()
            }

            override fun onSuccess(result: T) {
                onSuccess?.invoke(result)
                cancel()
            }

            override fun onFail(t: Throwable?) {
                super.onFail(t)
                onFail?.invoke(t?.message ?: "")
                cancel()
            }
        }
        ThreadUtils.executeBySingle(mTask)
    }

    @JvmStatic
    fun <T> executeByIo(doInBackground: () -> T, onSuccess: ((T) -> Unit)? = null, onFail: ((String) -> Unit)? = null, onDone: (() -> Unit)? = null) {
        val mTask = object : ThreadUtils.SimpleTask<T>() {
            override fun doInBackground(): T {
                return doInBackground.invoke()
            }

            override fun onSuccess(result: T) {
                onSuccess?.invoke(result)
                cancel()
            }

            override fun onFail(t: Throwable?) {
                super.onFail(t)
                onFail?.invoke(t?.message ?: "")
                cancel()
            }

            override fun onDone() {
                super.onDone()
                onDone?.invoke()
                cancel()
            }
        }
        ThreadUtils.executeByIo(mTask)
    }

    /**
     * 屏幕宽度
     */
    @JvmStatic
    fun getDialogWidth(rate: Float): Int {
        val mScreenWidth = ScreenUtils.getScreenWidth() - SizeUtils.dp2px(24F)
        val mScreenHeight = ScreenUtils.getScreenHeight() - SizeUtils.dp2px(24F)
        val mWidth: Int = if (mScreenWidth > mScreenHeight) {
            mScreenWidth
        } else {
            mScreenHeight
        }
        val mRateWidth = (mWidth * rate).toInt()
        return if (mRateWidth > mScreenWidth) {
            mScreenWidth
        } else {
            mRateWidth
        }
    }

    /**
     * dialog 需要全屏的时候用，和enableFocus() 成对出现
     * 在show 前调用  disableFocus   show后调用enableFocus
     *
     * @param window
     */
    @JvmStatic
    fun disableFocus(window: Window?) {
        window?.setFlags(
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        )
    }

    /**
     * dialog 需要全屏的时候用，disableFocus() 成对出现
     * 在show 前调用  disableFocus   show后调用enableFocus
     *
     * @param window
     */
    @JvmStatic
    fun enableFocus(window: Window?) {
        window?.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
    }

    /**
     * 全屏
     */
    @JvmStatic
    fun fullScreen(window: Window?) {
        if (window == null) {
            return
        }
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
        window.decorView.setOnSystemUiVisibilityChangeListener {
            var uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or  //布局位于状态栏下方
                    View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or  //全屏
                    View.SYSTEM_UI_FLAG_FULLSCREEN or  //隐藏导航栏
                    View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                    View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
            uiOptions = uiOptions or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            window.decorView.systemUiVisibility = uiOptions
        }
    }

}